<template>
	<view class="orders">
		<scroll-view class="scroll-view_H" scroll-x>
			<view class="slide_type_list">
				<view class="slide_type_list_view" v-for="(item,index) in typeList" :key="index"
					:class="{is_selected:active==index}" @click="changeType(item,index)">
					<view>{{item.name}}</view>
				</view>
			</view>
		</scroll-view>
		<view class="orderlists">
			<view class="content" v-for="(item,index) in orderList" :key="index">
				<view @click="goDetail(item)">
					<view class="ordernum">
						<text>订单号：{{item.serial_num}}</text>
						<text class="status">{{orderType[item.status].title}}</text>
					</view>
					<view class="shops">
						<image class="shop_img" :src="item.image"></image>
						<view class="shops_right">
							<view class="shop_info">
								<view class="shop_info_left">
									<text class="name">{{item.name}}</text>
								</view>
								<view class="shop_info_right">
									<view>￥{{item.total_price}}</view>
									<text>实付款</text>
								</view>
							</view>
							<view class="goodspec">
								<text class="tag" v-for="(tag,idx) in item.goods_specs_content"
									:key="idx">{{tag}}</text>
							</view>
							<view class="shop_deposit">
								<text>保证金:￥{{item.bond_price}}</text>
								<text class="final">尾款:￥{{item.tail_price}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="operate">
					<button type="default" size="mini" v-if="item.status == '3' || item.status == '4'"
						@click="goFlow(item)">查看物流</button>
					<button type="default" size="mini" v-if="item.status == '3'"
						@click="isConfirmGoods(item)">确认收货</button>
					<button type="default" size="mini" open-type="contact" class="red_btn"
						v-if="item.status == '2' && !version.embedded">联系客服</button>
					<button type="default" size="mini" class="red_btn" v-if="item.status == '1'"
						@click="goDetail(item)">立即支付</button>
					<button type="default" size="mini" v-if="item.status == '5'" @click="goDelete(item)">删除订单</button>
				</view>
			</view>

			<view v-show="isLoadMore && orderList.length >0">
				<uni-load-more :status="loadStatus"></uni-load-more>
			</view>

			<view class="nodata" v-if="orderList.length == 0">
				<image class="empty_img" :src="IMGLink+'/my/empty.png'" mode="widthFix"></image>
				<view>没有你想要的内容</view>
			</view>

		</view>
	</view>
</template>

<script>
	import { version } from "@/common/config.js"
	import {
		orderlist
	} from '@/api/my/orders.js';

	export default {
		data() {
			return {
				version: version,
				IMGLink: this.IMGLink,
				active: 0,
				typeList: [{
					name: '全部'
				}, {
					name: '待付款'
				}, {
					name: '待发货'
				}, {
					name: '待收货'
				}, {
					name: '已完成'
				}],
				orderList: [],
				orderType: {
					1: {
						title: "待付款",
					},
					2: {
						title: "待发货",
					},
					3: {
						title: "待收货",
					},
					4: {
						title: "已完成",
					},
					5: {
						title: "已关闭",
					},
					// 5 撤销订单
				},
				page: 1, //页码
				size: '10',
				status: "0",
				loadStatus: 'loading', //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
				isLoadMore: false, //是否加载中
				refresh: false, // 下拉刷新标示
				operate: "", //操作
			}
		},
		created() {},
		onBackPress(event){
			console.log(event)
		},
		onLoad(options) {
			if (Number(options.active) < 4) {
				this.active = Number(options.active) + 1;
			} else {
				this.active = 0;
			}
			this.status = this.active;
		},
		onShow() {
			this.getDataList();
		},
		methods: {
			// 列表
			getDataList() {
				uni.showLoading({
					title: '加载中'
				});
				orderlist('orderlist', {
					page: this.page,
					page_size: this.size,
					status: this.status
				}).then(res => {
					let datas = res.data.data;
					this.orderList = this.page == 1 ? datas : this.orderList.concat(datas);
					if (datas.length < this.size) { //小于size数，停止请求
						this.isLoadMore = true;
						this.loadStatus = 'nomore';
					} else {
						this.isLoadMore = false;
					}
					uni.hideLoading();
					if (this.refresh) {
						uni.stopPullDownRefresh();
						this.refresh = false;
					}
				}).catch(error => {
					uni.hideLoading();
					this.isLoadMore = false;
				})
			},
			changeType(item, index) {
				this.active = index;
				this.status = index;

				this.page = 1;
				this.getDataList();
			},
			onPullDownRefresh() {
				this.page = 1;
				this.getDataList();
				this.refresh = true;
			},
			onReachBottom() {
				if (!this.isLoadMore) { //此处判断，上锁，防止重复请求
					this.isLoadMore = true;
					this.page++;
					this.getDataList();
				}
			},
			// 订单详情页
			goDetail(item) {
				uni.navigateTo({
					url: `/pagesA/my/orders/orderDetail?serial_num=${item.serial_num}&id=${item.id}`
				})
			},
			// 去支付
			goPay(item) {
				uni.navigateTo({
					url: `/pagesA/my/orders/orderPay?serial_num=${item.serial_num}`
				})
			},
			// 查看物流
			goFlow(item) {
				uni.navigateTo({
					url: `/pagesA/my/orders/shopflow?serial_num=${item.serial_num}`
				})
			},
			// 确定收货
			isConfirmGoods(item) {
				this.operate = 'takegoods';
				this.showToast('确认收货吗？', item.id);
			},
			takeGoods(id) {
				uni.showLoading({
					title: '确认收货中···'
				});
				orderlist('confirm', {
					id: id
				}).then(res => {
					if (res.code == 200) {
						this.page = 1;
						this.getDataList();
					}
					uni.hideLoading();
				}).catch(error => {
					uni.hideLoading();
				})
			},
			// 弹框
			showToast(text, id) {
				uni.showModal({
					title: '提示',
					content: text,
					success: (res) => {
						if (res.confirm) {
							if (this.operate == 'takegoods') { //确认收货
								this.takeGoods(id);
							} else if (this.operate == 'deleted') {
								this.deleteOperate(id);
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 联系客服
			goContact() {
				uni.navigateTo({
					url: '/pagesA/my/customerservice/customerservice'
				})
			},
			// 删除订单
			goDelete(item) {
				this.operate = "deleted";
				this.showToast('确认要删除订单吗？', item.id);
			},
			deleteOperate(id) {
				uni.showLoading({
					title: '订单删除中···'
				});
				orderlist('delete', {
					id: id
				}).then(res => {
					if (res.code == 200) {
						this.page = 1;
						this.getDataList();
					}
					uni.hideLoading();
				}).catch(error => {
					uni.hideLoading();
				})
			},
		}
	}
</script>
<style>
	page {
		background-color: #EFF0F1;
	}
</style>
<style lang="scss" scoped>
	@import "../my.scss";

	// 横向滑动tab
	.scroll-view_H {
		position: fixed;
		left: 0;
		right: 0;
		top: 80rpx;
		/* #ifdef MP-WEIXIN */
		top: 0;
		/* #endif */
		z-index: 11;
		background-color: #FFFFFF;

		.slide_type_list {
			display: flex;
			padding: 20rpx 30rpx;

			.slide_type_list_view {
				width: 20%;
				padding-bottom: 10rpx;
				font-size: 28rpx;
				color: #0B0B0D;
				margin-right: 30rpx;

				view {
					text-align: center;
				}
			}

			.is_selected {
				color: #F54642;
				font-weight: 500;
				position: relative;
			}

			.is_selected:before {
				content: '';
				position: absolute;
				width: 44rpx;
				height: 8rpx;
				left: 50%;
				margin-left: -24rpx;
				bottom: -14rpx;
				background-color: #F54642;
			}
		}
	}

	.orderlists {
		margin-top: 120rpx;
	}

	.content {
		@include cards;
		background-color: #FFFFFF;
		padding: 30rpx 20rpx;
		margin: 30rpx 3% 20rpx;
		font-size: 28rpx;

		.ordernum {
			@include flexLayout(center, space-between);
			color: #595959;

			.status {
				color: #F54642;
			}
		}

		.shops {
			@include flexLayout(center, space-between);
			padding: 14rpx 0 30rpx;

			.shop_img {
				width: 138rpx;
				height: 154rpx;
				border-radius: 8rpx;
			}

			&_right {
				width: 75%;
				height: 154rpx;
				color: #0B0B0D;
				@include flexLayout(flex-start, space-between);
				flex-direction: column;

				.shop_info {
					width: 100%;
					@include flexLayout(flex-start, space-between);

					&_left {
						width: 70%;

						.name {
							@include exceedFlow(2);
						}
					}
				}

				.goodspec {
					.tag {
						display: inline-block;
						height: 42rpx;
						line-height: 42rpx;
						background-color: #F2F3F4;
						color: #ADADAD;
						font-size: 24rpx;
						padding: 0 16rpx;
						margin-top: 16rpx;
						margin-right: 10rpx;
					}
				}

				.shop_deposit {
					width: 100%;
					text-align: right;
					color: #5C5C5C;
					font-size: 24rpx;

					.final {
						margin-left: 30rpx;
					}
				}
			}
		}

		.operate {
			text-align: right;

			button {
				width: 188rpx;
				height: 54rpx;
				line-height: 54rpx;
				border-radius: 26rpx;
				border: 2rpx solid #B3B3B3;
				color: #464646;
				font-size: 28rpx;
				padding: 0;
				margin-left: 26rpx;
				background-color: #FFFFFF;
			}

			.red_btn {
				color: #F54642;
				border-color: #F54642;
			}
		}
	}

	.nodata {
		text-align: center;
		color: #7D7D7D;
		font-size: 24rpx;
		margin-top: 50%;

		.empty_img {
			width: 468rpx;
		}
	}
</style>
